package com.PVZ.utils;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.File;

/**
 * Created by @Author: kabuda on @Date: 2021/5/17 21:40
 * 音乐播放
 */
public class MusicPlayer {
    private Clip clip;

    public MusicPlayer(String filePath){
        File file = new File(filePath);

        AudioInputStream audio;

        try {
            audio = AudioSystem.getAudioInputStream(file);
            clip = AudioSystem.getClip();
            clip.open(audio);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *  播放
     * @param type -1 循环 0 不播放 1 播放一次
     */
    public void loop(int type){
        clip.loop(type);
    }
}
